package com.d5xx.hunters.events;

import com.d5xx.hunters.API.Hunter;
import com.d5xx.hunters.API.HunterGame;
import com.d5xx.hunters.API.HunterPlayer;
import org.bukkit.scheduler.BukkitRunnable;

import java.util.ArrayList;
import java.util.List;

import static com.d5xx.hunters.Variables.plugin;
import static com.d5xx.hunters.Variables.worldList;

public class GameWaiting {
    public static void waiting() {
        new BukkitRunnable() {
            @Override
            public void run() {

                // 判断游戏地图是否足够
                if (worldList.isEmpty()) { return; }
                // 判断人数是否足够
                if (Hunter.getWaitingPlayer().size() >= 1) {
                    List<HunterPlayer> gamers = new ArrayList<>();
                    if (Hunter.getWaitingPlayer().size() <= 5) {
                        gamers = Hunter.getWaitingPlayer();
                    }else {
                        for (int i = 0; i < 5; i++) {
                            gamers.add(Hunter.getWaitingPlayer().get(i));
                        }
                    }
                    HunterGame game = new HunterGame(gamers);
                    if (!game.ready()) {
                        for (HunterPlayer player : Hunter.getWaitingPlayer()) {
                            player.sendKeyMessage("HunterPlayer-start-error", true);
                            player. setType("lobby");
                        }
                        return;
                    }

                    // 休息5秒
                    new BukkitRunnable() {
                        @Override
                        public void run() {
                            game.start();
                            game.initMap();
                            Hunter.addGame(game.getWorlds(), game);
                        }
                    }.runTaskLaterAsynchronously(plugin, 5 * 20L);
                }
            }
        }.runTaskTimerAsynchronously(plugin, 0L, 40L);
    }

}
